﻿using SmallJob.Model;
using SmallJob.Services.Interfaces;
using SmallShop.Services.Implements;
using SmallShop.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmallShop.Services.Code
{
    public class MessageHelper
    {
        public static String BuildShortIDOrder(int orderid)
        {
            return orderid.ToString() + "O";
        }
        public static String BuildShortIDProvide(int provideid, int clientid)
        {
            return provideid.ToString() + "P" + clientid.ToString() + "C";
        }
        public static String BuildShortIDRequest(int requestid, int clientid)
        {
            return requestid.ToString() + "R" + clientid.ToString() + "C";
        }
        public static String BuildShortIDNormal(int people1id, int people2id)
        {
            return people1id.ToString() + "N" + people2id.ToString();
        }
        public static String BuildShortIDNotify(String shortid)
        {
            return "SN" + shortid;
        }
        public static String BuildShortIDComplaint(String shortid)
        {
            return "C" + shortid;
        }
        public static String GetShortIDRealParentNotify(String shortid)
        {
            if (shortid.Contains("SN"))
            {
               return shortid.Remove(0,2);
            }
            return shortid;
        }
        public static String GetStringSystemMessage(int type)
        {
            switch (type)
            {
                case (int)EEmbedInfoType.Payment: return "Thanh toán";
                case (int)EEmbedInfoType.Accept: return "Xác nhận đơn hàng";
                case (int)EEmbedInfoType.Delivery: return "Bàn giao";
                case (int)EEmbedInfoType.Rejected: return "Từ chối";
                default: return "Error";
            }
        }
        public static String BuildNotifyMessageContent(String originformat, DictionaryFormat dic)
        {
            EmailFactory factory = new EmailFactory();
            factory.DictionaryEmail = dic.GetDictionary();
            return factory.ReplaceWithDictionary(originformat);
        }
        public static void SendMessage(IUserService userservice, IMConversationService cvsservice, IMMessageService service, string content, string parentid, int receiverid, int senderid, int type, MMessageEmbedInfo info)
        {
            MMessage m = new MMessage()
              {
                  Content = content,
                  Date = DateTime.Now,
                  IsRead = false,
                  ParentID = parentid,
                  ReceiverID = receiverid,
                  SenderID = senderid,
                  Type = type,
                  Info = info
              };
            service.Add(m);
            if (type != (int)MessageType.System_Notify)
            {
                cvsservice.UpdateLastMessage(parentid, m);
            }
            //if (sendnotify)
            //{
            //    SendMessageSystem(cvsservice, service, content, MessageHelper.BuildShortIDNotify(parentid), receiverid, info);
            //}
        }
        public static void NewConversation(IMConversationService service, MMessage mes, Order order, Provide provide, Request request, User user1, User user2, string shortid, int type)
        {
            MConversation cvs = new MConversation()
            {
                Message = mes,
                Order = order == null ? null : MongoConvert.ConvertOrder(order, provide, request),
                Person1ID = user1.ID,
                Person2ID = user2.ID,
                ShortID = shortid,
                Type = type,
                Provide = (type == (int)MessageType.PROVIDE ? MongoConvert.ConvertProvide(provide) : null),
                Request = (type == (int)MessageType.REQUEST ? MongoConvert.ConvertRequest(request) : null),
                User1 = MongoConvert.ConvertUser(user1),
                User2 = MongoConvert.ConvertUser(user2)
            };
            service.Add(cvs);
        }
        public static void SendMessageNotifyMessage(IMMessageService service, string format,long numberunread,string flagtonotify, int receiverid,bool forcenotify)
        {
            List<long> lstfalg = new List<long>();
            foreach (var item in flagtonotify.Split(','))
            {
                lstfalg.Add(Convert.ToInt64(item));
            }
            if (forcenotify || lstfalg.Contains(numberunread) || numberunread >= lstfalg[lstfalg.Count - 1])
            {
                var factory = new EmailFactory();
                factory.GetDictionary().Add("count",numberunread.ToString());
                var notifycontent = factory.ReplaceWithDictionary(format);

                MMessage m = new MMessage()
                {
                    Content = notifycontent,
                    Date = DateTime.Now,
                    IsRead = false,
                    ParentID = MessageHelper.BuildShortIDNotify(""),
                    ReceiverID = receiverid,
                    SenderID = (int)EUserID.SystemUser,
                    Type = (int)MessageType.System_Notify
                };
                service.Add(m);
            }
        }
        public static void SendMessageComplaint(IMMessageService service, string content, string parentid, int senderid, MMessageEmbedInfo info)
        {
            MMessage m = new MMessage()
            {
                Content = content,
                Date = DateTime.Now,
                IsRead = false,
                ParentID = parentid,
                ReceiverID = (int)EUserID.SystemUser,
                SenderID = senderid,
                Type = (int)MessageType.Message_Complaint,
                Info = info
            };
            service.Add(m);
        }
        public static Boolean CheckIsShortIDOrder(string shortid)
        {
            return shortid.Contains("O");
        }
        public static string GetOrderID(string shortid)
        {
            if (CheckIsShortIDOrder(shortid))
            {
                return shortid.Remove(shortid.Length - 1, 1);
            }
            return "";
        }

        public static bool CheckIsShortIDRequest(string shortid)
        {
            return shortid.Contains("R");
        }
        public static string GetRequestID(string shortid)
        {
            if (CheckIsShortIDRequest(shortid))
            {
                return shortid.Substring(0,shortid.IndexOf('R'));
            }
            return "";
        }
        public static bool CheckIsShortIDProvide(string shortid)
        {
            return shortid.Contains("P");
        }
        public static string GetProvideID(string shortid)
        {
            if (CheckIsShortIDProvide(shortid))
            {
                return shortid.Remove(0, shortid.IndexOf('P'));
            }
            return "";
        }
    }
}
